package cn.itcast.solr;

import cn.itcast.bean.item;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class SolrJCreate {

    @Test
    public void testCreate() throws IOException, SolrServerException {
        //连接solr服务器
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建文档对象
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id","5");
        document.addField("title","波导手机,手机中的战斗机");

        //向solr服务器写入文档
        solrServer.add(document);

        //提交
        solrServer.commit();
    }


    @Test
    public void testCeate() throws IOException, SolrServerException {
        //连接solr服务器
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //创建javaBean对象
       /* item it = new item();
        it.setId("4");
       // it.setText("金品质,立天下");
        it.setTitle("金立:金品质,立天下");*/

        item it = new item();
        it.setId("2");
        it.setTitle("oppo:充电5分钟,通话2小时");
        it.setPrice(3999L);


        solrServer.addBean(it);
        solrServer.commit();

    }

    @Test
    public void testDelete() throws IOException, SolrServerException {
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //solrServer.deleteById("6");
        solrServer.deleteByQuery("title:vovovoovov");
        solrServer.commit();
    }

    @Test
    public void testQuery() throws SolrServerException {
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //初始化查询对象
        SolrQuery params = new SolrQuery("*:*");
        //执行查询,获得结果集
        QueryResponse query = httpSolrServer.query(params);
        System.out.println("一共获取:"+query.getResults().getNumFound()+"条数据");

        //处理结果集
        SolrDocumentList solrDocumentList = query.getResults();
        for (SolrDocument solrDocument : solrDocumentList) {
            System.out.println("id:"+solrDocument.getFieldValue("id"));
            System.out.println("title"+solrDocument.getFieldValue("title"));
            System.out.println("price"+solrDocument.getFieldValue("price"));
        }


    }

    @Test
    public void testQuery2() throws SolrServerException {
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //初始化查询对象
        SolrQuery params = new SolrQuery("*:*");
        QueryResponse response = httpSolrServer.query(params);
        List<item> beans = response.getBeans(item.class);

        System.out.println("一共获取"+response.getResults().getNumFound());
        beans.stream().forEach(System.out::println);


    }

    /**
     * 布尔查询
     * @throws SolrServerException
     */
    @Test
    public void testQuery3 () throws SolrServerException {
        //创建sorl服务对象
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //查询对象
        SolrQuery solrQuery  = new SolrQuery("title:oppo OR title:小米 ");
        //执行查询,获取结果集
        QueryResponse queryResponse = httpSolrServer.query(solrQuery);
        //解析响应结果集
        List<item> beans = queryResponse.getBeans(item.class);
        //System.out.println("");
        beans.forEach(System.out::println);


    }

    /**
     * 相似度查询
     * @throws SolrServerException
     */
    @Test
    public void testQuery4() throws SolrServerException {
        //连接olr服务器
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //查询对象
        SolrQuery solrQuery = new SolrQuery("title:oppe~");
        //执行查询
        QueryResponse queryResponse = httpSolrServer.query(solrQuery);
        //获取结果集数据
        List<item> beans = queryResponse.getBeans(item.class);
        beans.forEach(System.out::println);


    }

    @Test
    public void testQuery5() throws SolrServerException {
        //连接solr服务器
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //查询对象
        SolrQuery solrQuery = new SolrQuery("title:(id:2 OR id:3 OR id:5)");
        //执行查询
        QueryResponse queryResponse = httpSolrServer.query(solrQuery);
        //获取结果集数据
        List<item> beans = queryResponse.getBeans(item.class);
        beans.stream().forEach(System.out::println);

    }

    @Test
    public void testQuery6() throws SolrServerException {
        //连接solr服务器
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //查询对象
        SolrQuery solrQuery = new SolrQuery("*:*");
        solrQuery.setSort("price", SolrQuery.ORDER.desc);
        //执行查询
        QueryResponse queryResponse = httpSolrServer.query(solrQuery);
        //获取结果集数据
        List<item> beans = queryResponse.getBeans(item.class);
        beans.stream().forEach(System.out::println);


    }

    /**
     * 分页
     * @throws SolrServerException
     */
    @Test
    public void testQuery7() throws SolrServerException {
        //连接solr服务器
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //查询对象
        SolrQuery solrQuery = new SolrQuery("*:*");

        int curPage = 2; //当前页
        int pageSize = 3;
        int startIndex = (curPage -1) *pageSize; //🔛缩影
        solrQuery.setStart(startIndex);
        solrQuery.setRows(pageSize);


        //执行查询
        QueryResponse queryResponse = httpSolrServer.query(solrQuery);
        //获取结果集数据
        List<item> beans = queryResponse.getBeans(item.class);
        beans.stream().forEach(System.out::println);



    }

    /**
     * 高亮显示
     * @throws SolrServerException
     */
    @Test
    public void testQuery8() throws SolrServerException {
        //连接solr服务器
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //查询对象
        SolrQuery solrQuery = new SolrQuery("title:oppe~");
        //设置前置高亮
        solrQuery.setHighlightSimplePre("<em>");
        solrQuery.setHighlightSimplePost("</em>");
        //添加高亮字段
        solrQuery.addHighlightField("title");


        //执行查询
        QueryResponse queryResponse = httpSolrServer.query(solrQuery);
        //获取结果集数据
        List<item> beans = queryResponse.getBeans(item.class);
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
        beans.stream().forEach(item ->{
            System.out.println("id:"+item.getId());
            System.out.println("title:"+item.getTitle());
            System.out.println("price:"+item.getPrice());
            System.out.println("高亮的title:"+highlighting.get(item.getId()).get("title").get(0));

        });



    }























}
